package com.zhangmengyuan.medical.controller;

import com.zhangmengyuan.medical.entity.User;
import com.zhangmengyuan.medical.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired/*自动实例化*/
    private UserService userService;

    @PostMapping("/save")/*GET - 从指定的资源请求数据。
          •POST - 向指定的资源提交要被处理的数据*/

    //添加

    public String save(User user, Model model) {
        this.userService.save(user);
        return "redirect:/user/all";//跳转到
    }


    //更新
    @PostMapping("/save/{userId}")
    public String update(User user, @PathVariable("userId") Integer userId, Model model)/*路径传参*/
    {/*Intger代表类型，代表这个id必须是整数*/
        user.setUserId(userId);
        this.userService.save(user);
        return "redirect:/user/all";
    }

    @GetMapping("/all")
    public String getUser(Model model) {
        List<User> userList = this.userService.getAll();/*页面传参*/
   /* 在 JSP 视图页面中通过 request.getAttribute(“customerList”)
   或者通过 ${customerList} EL 表达式访问模型对象中的 customerList对象*/
        model.addAttribute("userList", userList);
        return "index";/*在方法上直接return一个字符串，就会对应webapp/WEB-INF.jsp/下的文件，不需要index.jsp*/
    }

    @GetMapping("/{userId}")
    public String getUserById(@PathVariable("userId") Integer userId, Model model) {
        User user = this.userService.getById(userId);
        model.addAttribute("user", user);
        return "redirect:/user/all";
    }

  /*@GetMapping("/delete/{userId}")
  public String deleteUserById(@PathVariable("userId") Integer userId,Model model){
    User user = this.userService.getById(userId);
    this.userService.deleteUserById(user);
    return "detil";

  }*/



}
